<template>
	<view class="container">
		<view class="main">
			<view v-if="addressList.length===0" class="empty">
				<view class="img">
					<image src="http://jiales.gz-yami.com/addr.png" />
				</view>
				<view class="txt">
					您还没有收货地址
				</view>
			</view>
			<radio-group class="radio-group">
				<block v-for="(item, index) in addressList" :key="index">
					<view class="address">
						<view class="personal" @tap="selAddrToOrder(item)">
							<view class="info-tit">
								<text class="name">
									{{ item.receiver }}
								</text>
								<text class="tel">
									{{ item.mobile }}
								</text>
								<image src="@/static/images/icon/revise.png" :data-addrid="item.addrId"
									@tap.stop="toEditAddress" />
							</view>
							<view class="addr">
								<text class="addr-get">
									{{ item.province }}{{ item.city }}{{ item.area }}{{ item.addr }}
								</text>
							</view>
						</view>
						<view class="select-btn" :data-addrId="item.addrId" @tap="onDefaultAddr">
							<view class="box">
								<radio :value="item.prodId" :checked="item.commonAddr==1" color="#eb2444"
									:data-addrid="item.addrId" @tap="onDefaultAddr" />
								设为默认地址
							</view>
						</view>
					</view>
				</block>
			</radio-group>
		</view>
		<view class="footer" @tap="onAddAddr">
			<text>新增收货地址</text>
		</view>
	</view>
</template>
<script>
	import number from "../../wxs/number";
	import {
		mineUserAddrList,
		editCommonAddr
	} from "../../api/mine.js"
	export default {
		data() {
			return {
				// addressList:[{receiver:22,mobile:15203210,province:"北京市",city:"市辖区",area:"东城区",addr:"12号"}],
				order: -1,
				addressList: []
			}
		},
		methods: {
			onLoad(option) {
				if (option.order) {
					this.order = option.order
				}
			},
			/**
			 * 加载地址列表
			 */
			onShow() {
				this.onGetList()
			},

			/**
			 * 获取收获列表接口
			 */
			onGetList() {
				uni.showLoading()
				mineUserAddrList()
					.then(({
						data
					}) => {
						console.log(data)
						this.addressList = data
						uni.hideLoading()
					})
			},

			/**
			 * 新增收货地址
			 */
			onAddAddr() {
				uni.navigateTo({
					url: '/pages/editAddress/editAddress'
				})
			},

			/**
			 * 设置为默认地址
			 */
			onDefaultAddr(e) {
				const addrId = e.currentTarget.dataset.addrid
				uni.showLoading()
				editCommonAddr(addrId)
					.then(() => {
						uni.hideLoading()
					})
			},
			/**
			 * 修改地址
			 */
			toEditAddress(e) {
				const addrId = e.currentTarget.dataset.addrid
				uni.navigateTo({
					url: '/pages/editAddress/editAddress?addrId=' + addrId
				})
			},

			/**
			 * 选择地址 跳转回提交订单页
			 */
			selAddrToOrder(item) {
				if (order.value == 0) {
					const pages = getCurrentPages() // 当前页面
					const prevPage = pages[pages.length - 2] // 上一页面
					prevPage.item = item // 直接给上一页面赋值
					prevPage.selAddress = 'yes'
					// 返回
					uni.navigateBack({
						delta: 1
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		background-color: #f4f4f4;
		border-top: 2rpx solid #e9eaec;
		min-height: 100vh;
		
	}

	.main {
		margin-top: 20rpx;
		padding-bottom: 150rpx;
	}

	.address {
		margin-bottom: 15rpx;
		width: 215%;
		background-color: #fff;
		border-bottom: 2rpx solid #e9eaec;

		.personal {
			position: relative;
			padding: 20rpx 30rpx;
			border-bottom: 3rpx dashed #e9eaec;

			.info-tit {
				.name {
					margin-right: 30rpx;
					font-size: 32rpx;
					display: inline-block;
				}

				.tel {
					font-size: 30rpx;
				}

				image {
					position: absolute;
					right: 30rpx;
					top: 46rpx;
					width: 40rpx;
					height: 40rpx;
					margin-left: 50rpx;
					vertical-align: middle;
				}
			}
		}

		.select-btn {
			padding: 15rpx 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.box {
				font-size: 26rpx;
			}
		}
	}

	.personal {
		.addr {
			font-size: 26rpx;
			margin: 10rpx 0;
			margin-top: 20rpx;

			.addr-get {
				display: inline-block;
				color: #999;
				width: 100%;
				word-break: break-word;
			}
		}
	}

	.footer {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		background-color: #fff;
		box-shadow: 0 -1rpx 8rpx rgba(0, 0, 0, 0.05);

		text {
			font-size: 32rpx;
			color: #eb2444;
		}
	}

	.empty {
		.img {
			text-align: center;
			margin-top: 130rpx;

			image {
				width: 100rpx;
				height: 100rpx;
				display: block;
				margin: auto;
			}
		}

		.txt {
			margin-top: 30rpx;
			font-size: 24rpx;
			text-align: center;
			color: #999;
		}
	}
</style>